/**
 * @description 与用户相关
 */
 import { setToken, removeToken } from "@/utils/jsCookie";
 import { login, getLoginInfo, logout, findUserMenu } from "@/api/user";
 import { filterAsyncRoutes } from "@/utils/handleMenu"
 import router from "@/router";
 
 
 export default {
     state: ()=> ({
         token: "",
         isLogin: false,
         userInfo: null,
         originRoutes: [],
         asyncRoutes: []
     }),
     getters: {
         originRoutes: (state) => state.originRoutes,
         getUserInfo: (state) => {
             return state.userInfo??{}
         }
     },
     mutations: {
         LOGIN_SUCCESS: (state, data) => {
             setToken(data.token);
             state.token = data.token;
             state.isLogin = true;
         },
         SET_USER_INFO: (state, data) => {
             state.userInfo = data;
         },
         SET_ASYNCROUTES: (state, data) => {
             state.asyncRoutes = data;
         },
         SET_ORIGINROUTES: (state, data) => {
             state.originRoutes = data;
         },
         CLEAR_USER_INFO: (state)=> {
             removeToken();
             state.userInfo = null;
             state.isLogin = false;
             state.originRoutes = [];
             state.asyncRoutes = [];
             router.getRoutes().forEach(route => {
                 if(!route?.meta?.isWhite){
                     route.name && router.hasRoute(route.name) && router.removeRoute(route.name);
                 }
             })
         }
     },
     actions: {
         // 登录认证
         login({ commit }, form){
             return new Promise((resolve, reject) => {
                 login(form).then(res=> {
                     if(res.code === 20000) {
                         commit('LOGIN_SUCCESS', res.data);
                         resolve(res)
                     } else {
                         reject(new Error(res.message??'登录认证失败！'))
                     }
                 })
             })
         },
     
         logout({ commit }){
             return new Promise((resolve, reject) => {
                 logout().then(res=> {
                     if(res.code === 20000) {
                         commit("CLEAR_USER_INFO");
                         resolve(res)
                     } else {
                         reject(new Error(res.message))
                     }
                 })
             })
         },
         
         // 获取当前登录用户信息
         getLoginInfo({ commit }) {
             return new Promise((resolve, reject)=> {
                 getLoginInfo().then(res => {
                     if(Number(res.code) === 20000) {
                         commit('SET_USER_INFO', res.data);
                         resolve(res);
                     } else {
                         reject(new Error(res.message || '获取用户信息失败！'))
                     }
                 })
             })
         },
     
         findUserMenu({ commit }) {
             return new Promise((resolve, reject) => {
                 findUserMenu().then(res=> {
                     if(res.code === 20000) {
                         const filterRes = filterAsyncRoutes(res?.data??[]);
                         commit('SET_ASYNCROUTES', filterRes);
                         commit('SET_ORIGINROUTES', res?.data??[]);
                         resolve(filterRes);
                         console.log(filterRes);
                     } else {
                         reject(new Error(res.message || "获取用户菜单失败！"))
                     }
                 })
             })
         },
         
         // 清除用户信息
         clearUserInfo({ commit }){
             return new Promise(((resolve) => {
                 commit("CLEAR_USER_INFO");
                 resolve();
             }))
         }
     }
 }